<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/2/24
 * Time: 11:39
 */

namespace app\admin\controller;
use phpDocumentor\Reflection\DocBlock\Tags\Var_;
use think\Config;
use think\Controller;
use think\Db;
use think\Request;
use think\Session;
use Common\Oss;

class Supplier extends Common
{

    /**
     * @writer:huangmingshuai
     * @DateTime:2018-02-24
     * @rewriter:xx
     * @rewriter_date:YY-MM-DD
     * @description: 商家列表接口
     * @return \think\response\Json
     */
    public function supplierList()
    {
        $post = Request::instance()->post();
        $page = $post['pageNumber'] ? $post['pageNumber'] : 1;
        $limit = $post['pageSize'] ? $post['pageSize'] : 10;
        $limit1 = ($page - 1) * $limit . ',' . $limit;
        $where = [];
        if(array_key_exists('city', $post['search']))
            $where['admin_name1|admin_name2|admin_name3'] = ['like','%'.$post['search']['city'].'%'];
        if(array_key_exists('tel', $post['search']))
            $where['manager_tel'] = ['like','%'.$post['search']['tel'].'%'];
        if(array_key_exists('name', $post['search']))
            $where['name'] = ['like','%'.$post['search']['name'].'%'];
        if(array_key_exists('manager', $post['search']))
            $where['manager'] = ['like','%'.$post['search']['manager'].'%'];
        $info = Db::name('supplier')
            ->field('id,name,image,company,admin_name2,is_open,manager,manager_tel')
            ->where(['check_status'=>2])
            ->where($where)
            ->order('id desc')
            ->limit($limit1)
            ->select();
        $count = Db::name('supplier')
            ->field('id,name,image,company')
            ->where(['check_status'=>2])
            ->where($where)
            ->count();
        if($info)
        {
            $result = Config::get('code.success');
            $result['data'] = $info;
            $result['count'] = $count;
        }
        else
        {
            $result = Config::get('code.null');
            $result['data'] = [];
            $result['count'] = 0;
        }
        return json($result);
    }

    /**
     * @writer:huangmingshuai
     * @DateTime:2018-02-24
     * @rewriter:xx
     * @rewriter_date:YY-MM-DD
     * @description: 商家详情
     * @return \think\response\Json
     */
    public function supplierDetail()
    {
        $id = Request::instance()->post('id');
        if ($id)
        {
            $info = Db::name('supplier')
                ->field('id,name,image,company,tel,company_code,post_code,address,manager,manager_tel,business as business_ids,account,password')
                ->where('id',$id)
                ->find();
            if($info)
            {
                $discount = Db::name('discount')->where([
                    'status' => 1,
                    'supplier_id' => $info['id']
                ])->value('type');
                if($discount == 1)
                {
                    $info['discount'] = '满减';
                }
                else
                {
                    $info['discount'] = '满送';
                }
                $info['business'] = Db::name('base_data')
                    ->field('id,name')
                    ->whereIn('id',$info['business_ids'])
                    ->where('parent_id',0)
                    ->select();
                $result = Config::get('code.success');
                $result['data'] = $info;
            }
            else
            {
                $result = Config::get('code.null');
            }
        }
        else
        {
            $result = Config::get('code.mistake');
        }
        return json($result);
    }

    /**
     * 商品详情接口
     */
    public function getProductInfo()
    {
        $id = Request::instance()->post('id');
        $info = Db::name('product')->field('id,name')->where('supplier',$id)->select();
        if($info)
        {
            $result = Config::get('code.success');
            $result['data'] = $info;
        }
        else
        {
            $result = Config::get('code.null');
        }
        return json($result);
    }

    /**
     * 上传图片
     * @return \think\response\Json
     */
    public function uploadImg()
    {
        $file = $_FILES;
        if($file)
        {
            $ossClient = new Oss();
            $files     = $ossClient->uploadFile('product', 'image');
            $img_path = join(",",$files);
            if($img_path)
            {
                $result = Config::get('code.success');
                $result['data'] = ['image' => $img_path];
            }
            else
            {
                $result = Config::get('code.error');
            }
        }
        else
        {
            $result = Config::get('code.mistake');
        }
        return json($result);
    }

    /**
     * @writer:huangmingshuai
     * @DateTime:2018-02-24
     * @rewriter:xx
     * @rewriter_date:YY-MM-DD
     * @description: 申请认证商家列表接口
     */
    public function supplierAuthList()
    {
        $post = Request::instance()->post();
        $page = $post['pageNumber'] ? $post['pageNumber'] : 1;
        $limit = $post['pageSize'] ? $post['pageSize'] : 10;
        $limit1= ($page - 1 ) * $limit . ',' . $limit;
        $where = [];
        if(array_key_exists('city', $post['search']))
            $where['admin_name1|admin_name2|admin_name3'] = ['like','%'.$post['search']['city'].'%'];
        if(array_key_exists('tel', $post['search']))
            $where['manager_tel'] = ['like','%'.$post['search']['tel'].'%'];
        if(array_key_exists('name', $post['search']))
            $where['name'] = ['like','%'.$post['search']['name'].'%'];
        if(array_key_exists('manager', $post['search']))
            $where['manager'] = ['like','%'.$post['search']['manager'].'%'];
        $info = Db::name('supplier')
            ->field('id,name,registration_time,company,manager,post_code,address,tel,company_code')
            ->where('check_status',1)
            ->where($where)
            ->order('id desc')
            ->limit($limit1)
            ->select();
        $count = Db::name('supplier')
            ->field('id,registration_time,company,manager,post_code,address,tel,company_code')
            ->where('check_status',1)
            ->where($where)
            ->count();
        if($info)
        {
            $result = Config::get('code.success');
            $result['data'] = $info;
            $result['count'] = $count;
        }
        else
        {
            $result = Config::get('code.null');
            $result['data'] = [];
            $result['count'] = 0;
        }
        return json($result);
    }
    
    /**
     * @writer:huangmingshuai
     * @DateTime:2018-02-24
     * @rewriter:xx
     * @rewriter_date:YY-MM-DD
     * @description: 关闭店铺
     */
    public function closeSupplier()
    {
        $data = Request::instance()->post();
        if(array_key_exists('id' , $data) && !empty($data['id']))
        {
            if(array_key_exists('type' , $data) && !empty($data['type']))
            {
                if($data['type'] == 'close')
                {
                    $status = Db::name('supplier')->where('id',$data['id'])->update(['is_open' => 0]);
                }
                else
                {
                    $status = Db::name('supplier')->where('id',$data['id'])->update(['is_open' => 1]);
                }
                if($status)
                {
                    $result = Config::get('code.success');
                }
                else
                {
                    $result = Config::get('code.error');
                }
            }
            else
            {
                $result = Config::get('code.mistake');
            }
        }
        else
        {
            $result = Config::get('code.mistake');
        }
        return json($result);
    }


    /**
     * @writer:huangmingshuai
     * @DateTime:2018-02-24
     * @rewriter:xx
     * @rewriter_date:YY-MM-DD
     * @description: 商家审核接口 type 1 表示通过  2 表示拒绝
     * @return \think\response\Json
     */
    public function authSupplier()
    {
        $data = Request::instance()->post();
        if(array_key_exists('type' , $data) && array_key_exists('id' , $data))
        {
            if($data['type'] == 1)//通过
            {
                $status = Db::name('supplier')->where('id',$data['id'])->update(['check_status' => 2 , 'is_open' => 1]);
                if($status)
                {
                    $result = Config::get('code.success');
                }
                else
                {
                    $result = Config::get('code.error');
                }
            }
            else if($data['type'] == 2)
            {
                if(array_key_exists('message' , $data))
                {
                    $status = Db::name('supplier')->where('id',$data['id'])->update(['check_status' => 3,'is_open' => 0]);
                    if($status)
                    {
                        $ins_data['message'] = $data['message'];
                        $ins_data['create_time'] = date('Y-m-d H:i:s');
                        $ins_data['supplier_id'] = $data['id'];
                        $status = Db::name('supplier_auth')->insert($ins_data);
                        if($status)
                        {
                            $result = Config::get('code.success');
                        }
                        else
                        {
                            $result = Config::get('code.error');
                        }
                    }
                    else
                    {
                        $result = Config::get('code.error');
                    }
                }
                else
                {
                    $result = Config::get('code.mistake');
                }
            }
            else
            {
                $result = Config::get('code.mistake');
            }
        }
        else
        {
            $result = Config::get('code.mistake');
        }
        return json($result);
    }

    /**
     * 修改商家信息
     * @return \think\response\Json
     */
    public function updateSupplierInfo()
    {
        $data = Request::instance()->post();
        if($data)
        {
            $save_data['name'] = $data['name'];
            $save_data['address'] = $data['address'];
            $save_data['company'] = $data['company'];
            $save_data['manager_tel'] = $data['manager_tel'];
            $save_data['tel'] = $data['tel'];
            $save_data['business'] = $data['business_ids'];
            $save_data['post_code'] = $data['post_code'];
            $save_data['manager'] = $data['manager'];
            $save_data['password'] = md5($data['password']);
            $save_data['image'] = $data['image'];
            Db::name('supplier')->where('id',$data['id'])->update($save_data);
            $result = Config::get('code.success');
        }
        else
        {
            $result = Config::get('code.mistake');
        }
        return json($result);
    }
}